package com.util.excel.toproperties;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import jxl.Cell;
import jxl.LabelCell;
import jxl.Sheet;
import jxl.Workbook;

public class ReadFaiceExcel
{
	public final static String FACEBOOK_ID = "_id";
	public final static String FACEBOOK_NAME = "name";
	public final static String FACEBOOK_DYNASTY = "dynasty";
	public final static String FACEBOOK_BIRTHYEAR = "birthyear";
	public final static String FACEBOOK_DEATHYEAR = "deathyear";
	public final static String FACEBOOK_LOYALTY = "loyalty";
	public final static String FACEBOOK_EVENT = "event";
	public final static String FACEBOOK_NAMEINDEX = "nameindex";
	public final static String FACEBOOK_INTRODUCTION = "introduction";
	public final static String FACEBOOK_IMGPATH = "imgpath";
	public final static String FACEBOOK_COLOR = "color";
	public final static String FACEBOOK_TRADE = "trade";
	public final static String FACEBOOK_PAINTMODES = "paintmodes";
	public final static String FACEBOOK_PATTERNSQUENCE = "patternsequence";
	public final static String FACEBOOK_PATTERNSTYLE = "patternstyle";

	public static void main(String[] args) throws Exception
	{

		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		final JFrame jFrame = new JFrame("Exce转换工具");

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

		double x = d.getWidth();
		double y = d.getHeight();

		jFrame.setSize(300, 50);

		x = (x - jFrame.getWidth()) / 2;
		y = (y - jFrame.getHeight()) / 2;

		jFrame.setLocation((int) x, (int) y);

		Container con = jFrame.getContentPane();
		con.setLayout(new GridLayout(1, 3));

		JButton jb = new JButton("浏览");
		JButton jc = new JButton("转换");

		final JLabel jl = new JLabel();

		jFrame.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosing(WindowEvent e)
			{
				super.windowClosing(e);
				System.exit(0);
			}

		});
		con.add(jl);
		con.add(jb);
		con.add(jc);
		jFrame.setVisible(true);

		jb.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO Auto-generated method stub
				JFileChooser jf = new JFileChooser();
				jf.addChoosableFileFilter(new FileFilterExcel());

				int id = jf.showOpenDialog(null);

				if (id == JFileChooser.OPEN_DIALOG)
				{
					String filePath = jf.getSelectedFile().getPath();

					if (null != filePath && filePath.length() > 0)
					{
						jl.setText(filePath);

					}

				}
			}
		});

		jc.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				String filePath = jl.getText().toLowerCase();

				if (null == filePath || filePath.length() <= 0)
				{
					return;
				}

				try
				{
					readFileToProperties(filePath);
					JOptionPane.showMessageDialog(null, "转换完成！");
				}
				catch (Exception e1)
				{
					JOptionPane.showMessageDialog(null, "转化出错！");
				}
			}
		});

	}

	public static void readFileToProperties(String filePath) throws Exception
	{
		File f = new File(filePath);
		Workbook workbook = Workbook.getWorkbook(f);
		Sheet[] sheets = workbook.getSheets();

		FileProperties values = FileProperties.getInstence(f.getParent() + File.separator + "proper.properties");

		for (Sheet s : sheets)
		{
			for (int i = 1; i < s.getRows(); i++)
			{
				Cell[] cells = s.getRow(i);

				if (null == cells || cells.length <= 0 || !(cells[0] instanceof LabelCell))
				{
					break;
				}

				Properties pro = new Properties();

				pro.put(FACEBOOK_NAME, cells[0].getContents());

				pro.put(FACEBOOK_DYNASTY, cells[1].getContents());

				pro.put(FACEBOOK_LOYALTY, cells[2].getContents());

				String[] birth = cells[3].getContents().split("-");
				pro.put(FACEBOOK_BIRTHYEAR, birth[0]);
				pro.put(FACEBOOK_DEATHYEAR, birth[1]);

				pro.put(FACEBOOK_EVENT, cells[4].getContents());

				pro.put(FACEBOOK_INTRODUCTION, cells[5].getContents());

				pro.put(FACEBOOK_COLOR, cells[6].getContents());

				pro.put(FACEBOOK_TRADE, cells[7].getContents());

				pro.put(FACEBOOK_PAINTMODES, cells[8].getContents());

				pro.put(FACEBOOK_PATTERNSQUENCE, cells[9].getContents());

				pro.put(FACEBOOK_PATTERNSTYLE, cells[10].getContents());

				pro.put(FACEBOOK_NAMEINDEX, cells[11].getContents());

				pro.put(FACEBOOK_IMGPATH, cells[12].getContents());

				values.put(pro);
			}
		}
		values.save();
	}

}

class FileFilterExcel extends FileFilter
{

	@Override
	public boolean accept(File f)
	{
		return f.getName().toLowerCase().contains(".xls");
	}

	@Override
	public String getDescription()
	{
		return null;
	}

}